package com.pesilo.module.navigate.mvc.model
{
	import com.pesilo.module.navigate.api.ApiLocator;
	import com.pesilo.module.navigate.literal.WeatherName;
	import com.pesilo.module.navigate.mvc.controller.ApplicationFacade;
	import com.pesilo.module.navigate.mvc.model.vo.InfoVO;
	import com.pesilo.mvc.model.vo.DataParamVO;
	import com.pesilo.net.http.HttpService;
	import com.pesilo.util.Logger;
	
	import org.puremvc.as3.multicore.interfaces.IProxy;
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;

	public class WeatherProxy extends Proxy implements IProxy
	{
		public static const NAME:String="WeatherProxy";
		
		private var infoVO:InfoVO;
		private var weather:String;
		private var type:int;
		public function WeatherProxy()
		{
			super(NAME);
		}
		
		public function requestData():void{
			getAddress();
		}
		
		private function getAddress():void
		{
			var pvo:DataParamVO=new DataParamVO(ApiLocator.API_WEATHER_URL,null);
			pvo.callbackEvent=__infoCallBackHandle;
			pvo.callErrorEvent=__infoCallErrorHandle;
			HttpService.getInstance().getData(null,pvo,true);
		}
		
		private function __infoCallErrorHandle(e:Object):void{
			Logger.logInfo("[Error] WeatherProxy Call Error");
			type=WeatherName.DISCONNECT;
		}
		
		private function __infoCallBackHandle(data:Object):void{
			infoVO=new InfoVO(data);
			
			weather=infoVO.weather;
			
			var rot:int=weather.indexOf("转");
			if(rot!=-1){
				weather=weather.substr(0,rot);
			}
			
			if(weather.indexOf(WeatherName.TYPE_SUN)!=-1){
				Logger.logInfo("[晴]",WeatherName.TYPE_SUN);
				type=WeatherName.SUN;
			}else if(weather.indexOf(WeatherName.TYPE_PARTLY_CLOUDY)!=-1){
				Logger.logInfo("[云]",WeatherName.TYPE_PARTLY_CLOUDY);
				type=WeatherName.PARTLY_CLOUDY;
			}else if(weather.indexOf(WeatherName.TYPE_CLOUDY)!=-1){
				Logger.logInfo("[阴]",WeatherName.TYPE_CLOUDY);
				type=WeatherName.CLOUDY;
			}else if(weather.indexOf(WeatherName.TYPE_MINE)!=-1){
				Logger.logInfo("[雷]",WeatherName.TYPE_MINE);
				type=WeatherName.MINE;
			}else if(weather.indexOf(WeatherName.TYPE_DRIZZLE)!=-1){
				Logger.logInfo("[小雨]",WeatherName.TYPE_DRIZZLE);
				type=WeatherName.DRIZZLE;
			}else if(weather.indexOf(WeatherName.TYPE_RAIN)!=-1){
				Logger.logInfo("[雨]",WeatherName.TYPE_RAIN);
				type=WeatherName.RAIN;
			}else if(weather.indexOf(WeatherName.TYPE_SNOW)!=-1){
				Logger.logInfo("[小雪]",WeatherName.TYPE_SNOW);
				type=WeatherName.SNOW;
			}else if(weather.indexOf(WeatherName.TYPE_BLIZZARD)!=-1){
				Logger.logInfo("[雪]",WeatherName.TYPE_BLIZZARD);
				type=WeatherName.BLIZZARD;
			}else if(weather.indexOf(WeatherName.TYPE_HAIL)!=-1){
				Logger.logInfo("[冰雹]",WeatherName.TYPE_HAIL);
				type=WeatherName.HAIL;
			}else{
				Logger.logInfo("[NULL-DISCONNECT]");
				type=WeatherName.DISCONNECT;
			}
			
			facade.sendNotification(ApplicationFacade.APP_WEATHER_TYPE_EVENT,type);
		}
	}
}